package com.lebang.service.buss;

import com.lebang.entity.member.LbbMember;
import com.lebang.entity.member.LbbMemberFeedback;
import com.lebang.mapper.LbbMemberFeedbackMapper;
import com.lebang.mapper.LbbMemberMapper;
import com.lebang.mapper.SendEmailMapper;
import com.lebang.model.enums.Booleans;
import com.lebang.model.vo.buss.MemberFeedbackVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.*;

@Service
public class MemberFeedbackService {
    @Autowired
    private LbbMemberFeedbackMapper lbbMemberFeedbackMapper;
    //@Autowired
  //  private JavaMailSender javaMailSender;
    @Autowired
    private SendEmailMapper sendEmailMapper;
    @Autowired
    private LbbMemberMapper lbbMemberMapper;
    @Value("${info.name}")
    private String appName;

    public Map<String, Object> list(MemberFeedbackVo vo) {
        Map<String, Object> data = new HashMap<String, Object>(1);
        Integer total = lbbMemberFeedbackMapper.total(vo);
        List<Map> rows = lbbMemberFeedbackMapper.pagging(vo);
        data.put("total", total);
        data.put("rows", rows);
        return data;
    }


    /**
     * 获取当天回馈次数
     *
     * @param userid
     */
    public int count(String createTime, String userid) {
        return lbbMemberFeedbackMapper.count(createTime, userid);
    }

    /**
     * 回馈界面
     *
     * @param userid
     * @return
     */
    public Map<String, Object> hasCompleted(String userid) {
        Map<String, Object> data = new LinkedHashMap<String, Object>();
        List<Map<String, Object>> rows = lbbMemberFeedbackMapper.hasCompleted(userid);
        data.put("rows", rows);
        return data;
    }



    public LbbMemberFeedback getByFeedBackId(String feedBackId) {
        return lbbMemberFeedbackMapper.getByFeedBackId(feedBackId);
    }

    public int updateByPrimaryKeySelective(LbbMemberFeedback lbbMemberFeedback) {
        return lbbMemberFeedbackMapper.updateByPrimaryKeySelective(lbbMemberFeedback);
    }

    public int insertSelective(LbbMemberFeedback lbbMemberFeedback,Integer appId) {
        lbbMemberFeedback.setHasDeleted(Booleans.False.name());
        lbbMemberFeedback.setCreateTime(new Date());
        int i = lbbMemberFeedbackMapper.insertSelective(lbbMemberFeedback);
        if (i > 0){
            try {
                //发送邮件
                this.sendEmail(lbbMemberFeedback,appId);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return i;
    }

    /**
     * 发邮件
     */
    @Async
    public void sendEmail(LbbMemberFeedback lbbMemberFeedback,Integer appId) {
        //查询需要发送的邮箱
        List<String> list=sendEmailMapper.selectByUserClass(appId);
        SimpleMailMessage message =new SimpleMailMessage();
        message.setFrom("service@gara.cc");
        message.setTo(list.toArray(new String[]{}));
        message.setSubject(appName+"用户反馈");
        LbbMember member = lbbMemberMapper.getMemberByMemberId(lbbMemberFeedback.getUserId());
        String text="Id:"+lbbMemberFeedback.getId()+"\n"+"时间:"+ LocalDate.now()+"\n" +"手机号:"+member.getPhone()+"\n"+"反馈类容:"+lbbMemberFeedback.getContent();
        message.setText(text);
        //javaMailSender.send(message);
    }
}